package com.gitee.xhs.saveAndModifyingPointWriter.keyedState.reducingState;

import com.gitee.xhs.keyedState.reducingState.ReducingStateConstant;
import com.gitee.xhs.sampleRecod.transactionRecord.Transaction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.state.api.functions.KeyedStateBootstrapFunction;

/**
 * @author BigHead
 * @date 2022/7/12 11:13
 * @description ReducingStateNewSavePointsFunction
 */
public class ReducingStateNewSavePointsFunction extends KeyedStateBootstrapFunction<Long, Transaction> {

    private transient ReducingState<Double> reducingState;

    @Override
    public void open(Configuration configuration) throws Exception {
        reducingState = getRuntimeContext().getReducingState(
                ReducingStateConstant.REDUCING_STATE_DESCRIPTOR
        );
    }

    @Override
    public void processElement(Transaction value, KeyedStateBootstrapFunction<Long, Transaction>.Context ctx) throws Exception {
        reducingState.add(value.getAmount());
    }
}
